package cn.s1995.sloth.app.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.s1995.sloth.bizs.oo.req.VideoCommentREQ;
import cn.s1995.sloth.bizs.oo.vo.VideoCommentVO;
import cn.s1995.sloth.bizs.service.VideoCommentService;
import cn.s1995.sloth.bizs.service.common.CacheService;
import cn.s1995.sloth.common.core.constant.enums.hint.Hint;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.security.annotation.Inner;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/m/videoComment")
@Tag(description = "VideoComment", name = "APP-视频评论模块")
public class ApiVideoCommentController {

	private final VideoCommentService videoCommentService;

	private final CacheService cacheService;

	@Autowired
	private HttpServletRequest request;

	@Inner(false)
	@ApiOperationSupport(order = 1)
	@Operation(summary = "分页查询评论", description = "分页查询评论 参数videoId、pid")
	@Parameters({
	   @Parameter(name = "videoId",description = "视频id", required = false, in = ParameterIn.DEFAULT),
	   @Parameter(name = "pid",description = "父评论id，默认0", required = false, in = ParameterIn.DEFAULT),
	})
	@GetMapping("/comment/page")
	public R<Page<VideoCommentVO>> getCommentPage(@ParameterObject Page page, @ParameterObject VideoCommentREQ req) {
		if(ObjectUtil.isNull(req.getVideoId()) && ObjectUtil.isNull(req.getPid()))
			return R.failed(Hint.BAD_PARAMETER);
		if(ObjectUtil.isNull(req.getPid()))
			req.setPid(0L);
		Long uid = cacheService.getUidByToken(request.getHeader(HttpHeaders.AUTHORIZATION));
		Page<VideoCommentVO> resultPage = videoCommentService.getCommentPage(page, req);
		if(ObjectUtil.isNotNull(uid) && ObjectUtil.isNotEmpty(resultPage.getRecords())){
			for (VideoCommentVO vo : resultPage.getRecords()) {
				videoCommentService.setLikeStatus(uid, vo);
			}
		}
		return R.ok(resultPage);
	}

	@ApiOperationSupport(order = 2)
    @Operation(summary = "发布视频评论", description = "发布视频评论")
	@PostMapping("/comment/save")
	public R saveComment(@RequestBody VideoCommentREQ req) {
		videoCommentService.add(req);
		return R.ok();
	}

	@ApiOperationSupport(order = 3)
    @Operation(summary = "点赞评论", description = "点赞评论")
	@Parameters({
	   @Parameter(name = "id",description = "评论id", required = true, in = ParameterIn.PATH),
	})
	@PostMapping("/like/{id}")
	public R likeVideo(@PathVariable("id") Long id) {
		return videoCommentService.like(id);
	}
}
